<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="250px">
      <el-form-item label="学号" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="年级" prop="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入年级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable style="width: 250px">
          <el-option
            v-for="dict in gender"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> 

      
      <el-form-item label="学院" prop="collegeId">
        <el-select v-model="queryParams.collegeId" placeholder="请选择学院" clearable style="width: 250px">
          <el-option
            v-for="item in collegeList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择班级" clearable style="width: 250px">
          <el-option
            v-for="item in classList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item label="专业" prop="majorId">
        <el-select v-model="queryParams.majorId" placeholder="请选择专业" clearable style="width: 200px">
          <el-option
            v-for="item in majorList"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
          />
        </el-select>
      </el-form-item> 

      <el-form-item label="辅导员" prop="counselor">
        <el-input
          v-model="queryParams.counselor"
          placeholder="请输入辅导员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    
      <el-form-item label="班导师" prop="classAdvisor">
        <el-input
          v-model="queryParams.classAdvisor"
          placeholder="请输入班导师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> 
        
       
      <el-form-item label="就业指导老师" prop="careerCounselor">
        <el-input
          v-model="queryParams.careerCounselor"
          placeholder="请输入就业指导老师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> 
      <!-- <el-form-item label="生源所在地" prop="originPlace">
        <el-input
          v-model="queryParams.originPlace"
          placeholder="请输入生源所在地"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="困难群体帮扶老师" prop="supportTeacher">
        <el-input
          v-model="queryParams.supportTeacher"
          placeholder="请输入困难群体帮扶老师"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:Students:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:Students:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:Students:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:Students:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="StudentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template #default="scope">
          <dict-tag :options="gender" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="民族" align="center" prop="ethnicity" />
      <el-table-column label="政治面貌" align="center" prop="politicalStatus" />
      <el-table-column label="学院" align="center" prop="college.deptName" />
      <el-table-column label="专业" align="center" prop="major.deptName" />
      <el-table-column label="班级" align="center" prop="classDept.deptName" />
      <el-table-column label="辅导员" align="center" prop="counselor" />
      <el-table-column label="班导师" align="center" prop="classAdvisor" />
      <el-table-column label="就业指导老师" align="center" prop="careerCounselor" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="QQ号" align="center" prop="qqNumber" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="生源所在地" align="center" prop="originPlace" />
      <el-table-column label="家庭住址" align="center" prop="homeAddress" />
      <el-table-column label="家庭电话" align="center" prop="homePhone" />
      <el-table-column label="困难群体帮扶老师" align="center" prop="supportTeacher" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:Students:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:Students:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="StudentsRef" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="学号" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in gender"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="ethnicity">
          <el-input v-model="form.ethnicity" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalStatus">
          <el-input v-model="form.politicalStatus" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="学院" prop="collegeId">
          <!-- <el-input v-model="form.collegeId" placeholder="请输入学院ID" /> -->
           <el-select v-model="form.collegeId" placeholder="请选择学院">
            <el-option
              v-for="item in collegeList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业" prop="majorId">
          <!-- <el-input v-model="form.majorId" placeholder="请输入专业ID" /> -->
           <el-select v-model="form.majorId" placeholder="请选择专业">
              <el-option
              v-for="item in majorList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <!-- <el-input v-model="form.classId" placeholder="请输入班级" /> -->
           <el-select v-model="form.classId" placeholder="请选择班级">
            <el-option
              v-for="item in classList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="辅导员" prop="counselor">
          <el-input v-model="form.counselor" placeholder="请输入辅导员" />
        </el-form-item>
        <el-form-item label="班导师" prop="classAdvisor">
          <el-input v-model="form.classAdvisor" placeholder="请输入班导师" />
        </el-form-item>
        <el-form-item label="就业指导老师" prop="careerCounselor">
          <el-input v-model="form.careerCounselor" placeholder="请输入就业指导老师" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="QQ号" prop="qqNumber">
          <el-input v-model="form.qqNumber" placeholder="请输入QQ号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="生源所在地" prop="originPlace">
          <el-input v-model="form.originPlace" placeholder="请输入生源所在地" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="家庭电话" prop="homePhone">
          <el-input v-model="form.homePhone" placeholder="请输入家庭电话" />
        </el-form-item>
        <el-form-item label="困难群体帮扶老师" prop="supportTeacher">
          <el-input v-model="form.supportTeacher" placeholder="请输入困难群体帮扶老师" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Students">
import { listStudents, getStudents, delStudents, addStudents, updateStudents } from "@/api/system/Students";
import { listDept } from "@/api/system/dept";
import {getEmploymentStatsAndIntent} from "@/api/system/EmploymentInfo";
import useUserStore from '@/store/modules/user'

const { proxy } = getCurrentInstance();
const { gender } = proxy.useDict('gender');

const StudentsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const userStore = useUserStore();

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    //学生表中信息
    id: null,
    studentId: null,
    name: null,
    gender: null,
    collegeId: null,
    majorId: null,
    classId: null,
    counselor: null,
    classAdvisor: null,
    careerCounselor: null,
    originPlace: null,
    supportTeacher: null,
    // 就业表中信息
    employment_intent: null,//就业意向
    employment_status: null,//就业状态
    current_company: null,//就业公司
    is_on_campus: null,//是否在校
    province: null,//就业所在省
    //就业追踪表中信息
    contact_month:null, //联系月份
    employment_record: null //就业记录
  },
  rules: {
    studentId: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);
const uid = userStore.id;
/** 查询学生信息列表 */
function getList() {
  loading.value = true;
  listStudents(
    queryParams.value,
    uid
  ).then(response => {
    StudentsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 查询学院信息列表 */
// 封装响应式对象
const collegeParams = reactive({
    pageNum: 1,
    pageSize: 100,
    parentId: 100
});
// 定义返回结果列表
const collegeList = ref([]);
// 调用方法
function getCollegeList() {
  listDept({ pageNum: collegeParams.pageNum, pageSize: collegeParams.pageSize, parentId: collegeParams.parentId}).then(response => {
    collegeList.value = response.data;
    
  });
}


/** 查询专业信息列表 */
// 封装响应式对象
const majorParams = reactive({
    pageNum: 1,
    pageSize: 1000,
    ancestors: 100
});
// 定义返回结果列表
const majorList = ref([]);

// 调用方法
function getMajorList() {
  listDept({ pageNum: majorParams.pageNum, pageSize: majorParams.pageSize, ancestors: majorParams.ancestors}).then(response => {
    majorList.value = response.data;
  })
}

// 查询班级信息列表 */
// 封装响应式对象
const classParams = reactive({
    pageNum: 1, 
    pageSize: 1000,
    ancestors: 200
});
// 定义返回结果列表
const classList = ref([]);

// 调用方法
function getClassList() {
  listDept({ pageNum: classParams.pageNum, pageSize: classParams.pageSize, ancestors: classParams.ancestors}).then(response => {
    classList.value = response.data;
  })
}



// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    //学生表中信息
    id: null,
    grade: null,
    studentId: null,
    name: null,
    gender: null,
    collegeId: null,
    majorId: null,
    classId: null,
    counselor: null,
    classAdvisor: null,
    careerCounselor: null,
    originPlace: null,
    supportTeacher: null,
    // 就业表中信息
    employment_intent: null,//就业意向
    employment_status: null,//就业状态
    current_company: null,//就业公司
    is_on_campus: null,//是否在校
    province: null,//就业所在省
    //就业追踪表中信息
    contact_month:null, //联系月份
    employment_record: null //就业记录
  };
  proxy.resetForm("StudentsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.studentId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学生信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _studentId = row.studentId || ids.value
  getStudents(_studentId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学生信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["StudentsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStudents(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStudents(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _studentIds = row.studentId || ids.value;
  proxy.$modal.confirm('是否确认删除学生信息编号为"' + _studentIds + '"的数据项？').then(function() {
    return delStudents(_studentIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hour = String(now.getHours()).padStart(2, '0');
  const minute = String(now.getMinutes()).padStart(2, '0');
  const fileName = `学生信息_${year}${month}${day}_${hour}${minute}.xlsx`;

  proxy.download(`system/Students/export/${uid}`, {
    ...queryParams.value
  }, fileName)
}

getClassList();
getMajorList();
getCollegeList();
getList();
</script>
